home *** CD-ROM | disk | FTP | other *** search
- Path: surfnet.nl!sun4nl!xs4all!falstaff
- From: falstaff@xs4all.nl (Falstaff)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with scanf
- Date: 22 Mar 1996 22:27:46 GMT
- Organization: XS4ALL, networking for the masses
- Message-ID: <4iv9h2$5a3@news.xs4all.nl>
- References: <4iscgb$8h9@newsflash.concordia.ca>
- NNTP-Posting-Host: xs1.xs4all.nl
- X-Newsreader: NN version 6.5.0 #666 (NOV)
-
- v_ruso@alcor.concordia.ca writes:
-
- >I am writing a program that is supposed to gather information from the
- >user.
- >The program works fine if the user enters some numbers and presses
- >enter. But if the user just presses enter without entering any data,
- >the cursor just starts a new line.
- >Can anyone tell me how I can avoid this?
-
- You can use fgets() instead to read a line of text from the user
- up to the next newline into a buffer, then process the contents
- of the buffer (for example, using sscanf).
- Note that if the user types more than the buffer can hold,
- the string does not end with a linefeed, and you should
- throw the extra input away (perhaps with a while(getchar()) loop.
-
- I *never* use fgets() in interactive programs, instead I use
- a GetString() function I wrote that limits the user to typing less
- than some specified number of characters (fgets won't stop the user
- typing too much, even though it prevents the buffer from overflowing).
-
- Frank
- --
- The famous GIICM now on line: http://www.xs4all.nl/~falstaff/GIICM.html
- ------------------------------------------------------------------------
- Frank A. Vorstenbosch +31-(70)-355 5241 falstaff@xs4all.nl
-